import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/page/auto_makers/shop_home/order_details/order_info.dart';
import 'package:ytbusiness/page/auto_makers/shop_home/order_widget/enquiry_item.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';

//待结算
class AwaitSettlement extends StatefulWidget {
  @override
  _AwaitSettlementState createState() => _AwaitSettlementState();
}

class _AwaitSettlementState extends State<AwaitSettlement> {
  List<String> typeList = ["原厂原包", "品牌", "拆车", "OEM"];
  Map<String, dynamic> deliveryInfo = {
    "deliveryTime": "2020-11-27 11:21",
    "deliveryType": "韵达",
    "deliveryNumber": "2020154654546546665"
  }; //发货信息
  bool deliveryInfoShow = false; //显示隐藏发货信息
  bool invoiceInfoShow = false; //显示隐藏回票信息
  bool isShow = false; //显示隐藏已发货商品
  int checkIndex = 0; //当前选中索引
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: YTStyle.bgColor,
        appBar: customAppBar(context, title: "待结算"),
        body: CustomScrollView(
          slivers: [
            //订单信息
            SliverToBoxAdapter(
              child: OrderInfo(),
            ),

            //联系方式
            SliverToBoxAdapter(
              child: Container(
                padding:
                    EdgeInsets.only(bottom: setWidth(10), top: setWidth(10)),
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    link(image: "blue_kefu.png", title: "在线联系", phone: "10086"),
                    link(
                        image: "blue_phone_link.png",
                        title: "电话联系",
                        phone: "10086"),
                  ],
                ),
              ),
            ),

            //询价信息
            SliverToBoxAdapter(
              child: Container(
                margin: EdgeInsets.only(top: setWidth(15)),
                padding: EdgeInsets.only(
                    top: setWidth(18),
                    left: setWidth(15),
                    bottom: setWidth(13)),
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                        bottom: BorderSide(
                            color: Color(0xffF0F0F0), width: setWidth(0.5)))),
                child: Text(
                  "询价信息",
                  style: TextStyle(
                      color: Color(0xff323232),
                      fontSize: setSp(15),
                      fontWeight: FontWeight.w600),
                ),
              ),
            ),

            //询价列表
            SliverList(
                delegate: SliverChildBuilderDelegate(
              (context, index) {
                return EnquiryItem(
                  checkIndex: checkIndex,
                  onCheckUpdate: (value) {
                    print(value);
                  },
                );
              },
              //默认显示一条
              childCount:
                  isShow ? (typeList.length > 0 ? typeList.length : 0) : 1,
            )),

            //隐藏显示发货商品
            SliverToBoxAdapter(
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  setState(() {
                    isShow = !isShow;
                  });
                },
                child: Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(bottom: setWidth(12)),
                  padding:
                      EdgeInsets.only(top: setWidth(12), bottom: setWidth(12)),
                  color: Colors.white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          isShow
                              ? "隐藏待结算的${typeList.length - 1}个商品"
                              : "已隐藏待结算的${typeList.length - 1}个商品",
                          style: TextStyle(
                              color: Color(0xff989898), fontSize: setSp(14)),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(
                            left: setWidth(6), right: setWidth(6)),
                        child: Text(
                          isShow ? "收起" : "展开",
                          style: TextStyle(
                              color: Color(0xff989898),
                              fontSize: setSp(14),
                              fontWeight: FontWeight.w600),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(12),
                        height: setWidth(6),
                        child: Image.asset(
                          isShow
                              ? "assets/images/shouqi.png"
                              : "assets/images/zhankai.png",
                          width: setWidth(12),
                          height: setWidth(6),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            //结算信息
            SliverToBoxAdapter(
              child: settlementInfo(
                  price: "1259.00", serve: "3280.00", sum: "1259.00"),
            ),
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(12),
              ),
            ),

            //发货信息
            SliverToBoxAdapter(
              child: Container(
                padding: EdgeInsets.only(
                  top: setWidth(18),
                  left: setWidth(15),
                  right: setWidth(15),
                ),
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Text(
                            "发货信息",
                            style: TextStyle(
                                color: Color(0xff323232),
                                fontSize: setSp(16),
                                fontWeight: FontWeight.w600),
                          ),
                        ),
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            setState(() {
                              deliveryInfoShow = !deliveryInfoShow;
                            });
                          },
                          child: Container(
                            //
                            padding: EdgeInsets.all(10),
                            child: SizedBox(
                              width: setWidth(12),
                              height: setWidth(6),
                              child: Image.asset(
                                deliveryInfoShow
                                    ? "assets/images/shouqi.png"
                                    : "assets/images/zhankai.png",
                                width: setWidth(12),
                                height: setWidth(6),
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    //运单列表
                    Container(
                      child: ListView.builder(
                          physics: NeverScrollableScrollPhysics(),
                          //默认显示一条
                          itemCount: deliveryInfoShow
                              ? (deliveryInfo.length > 0
                                  ? deliveryInfo.length
                                  : 0)
                              : 1,
                          shrinkWrap: true,
                          itemBuilder: (context, index) {
                            return _deliveryItem(deliveryInfo);
                          }),
                    ),
                  ],
                ),
              ),
            ),

            //回票信息
            SliverToBoxAdapter(
              child: Container(
                margin: EdgeInsets.only(top: setWidth(15)),
                padding: EdgeInsets.only(
                  top: setWidth(18),
                  left: setWidth(15),
                  right: setWidth(15),
                ),
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      margin: EdgeInsets.only(bottom: setWidth(10)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Text(
                              "开票信息",
                              style: TextStyle(
                                  color: Color(0xff323232),
                                  fontSize: setSp(16),
                                  fontWeight: FontWeight.w600),
                            ),
                          ),
                          GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {
                              setState(() {
                                invoiceInfoShow = !invoiceInfoShow;
                              });
                            },
                            child: Container(
                              padding: EdgeInsets.all(10),
                              child: SizedBox(
                                width: setWidth(12),
                                height: setWidth(6),
                                child: Image.asset(
                                  invoiceInfoShow
                                      ? "assets/images/shouqi.png"
                                      : "assets/images/zhankai.png",
                                  width: setWidth(12),
                                  height: setWidth(6),
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    //回票信息
                    Container(
                      child: invoiceInfoShow
                          ? _backInvoice()
                          : _deliveryItem(deliveryInfo),
                    ),
                  ],
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(22),
              ),
            )
          ],
        ),
      ),
    );
  }

  //回票信息
  Widget _backInvoice() {
    return Column(
      children: [
        invoiceInfo("开票金额:", "34792.00（含税）"),
        invoiceInfo("发票抬头:", "太平洋保险股份有限公司安徽合肥分公司"),
        invoiceInfo("税号:", "983798oqiuwqu2323"),
        copyInfo("地址:", "安徽省合肥市"),
        copyInfo("邮寄地址:", "某某某 12545689756 安徽省合肥市庐阳区万科中心2154"),
        _deliveryItem(deliveryInfo)
      ],
    );
  }

  //拷貝发票信息
  Widget copyInfo(String title, String content) {
    return Container(
      margin: EdgeInsets.only(bottom: setWidth(18)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: setWidth(70),
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(15)),
            ),
          ),
          Expanded(
            child: Container(
              child: Text(content,
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(15))),
            ),
          ),
          SizedBox(
            width: setWidth(31),
          ),
          GestureDetector(
            onTap: () {
              copy(content);
            },
            child: Container(
              child: Text(
                "复制",
                style: TextStyle(color: Color(0xff1666F1), fontSize: setSp(15)),
              ),
            ),
          ),
        ],
      ),
    );
  }

  //发票信息
  Widget invoiceInfo(String title, String content) {
    return Container(
      margin: EdgeInsets.only(bottom: setWidth(18)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: setWidth(64),
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(14)),
            ),
          ),
          Expanded(
            child: Container(
              child: Text(content,
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(14))),
            ),
          ),
        ],
      ),
    );
  }

  Widget _deliveryItem(Map<String, dynamic> deliveryInfo) {
    return Container(
      margin: EdgeInsets.only(top: setWidth(20), bottom: setWidth(20)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //发货时间
          Container(
            margin: EdgeInsets.only(bottom: setWidth(18)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: setWidth(64),
                  child: Text(
                    "发货时间:",
                    style: TextStyle(
                        color: Color(0xff323232), fontSize: setSp(14)),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Text(deliveryInfo["deliveryTime"] as String,
                        style: TextStyle(
                            color: Color(0xff323232), fontSize: setSp(14))),
                  ),
                ),
              ],
            ),
          ),
          //快递方式
          Container(
            margin: EdgeInsets.only(bottom: setWidth(18)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: setWidth(64),
                  child: Text(
                    "快递方式:",
                    style: TextStyle(
                        color: Color(0xff323232), fontSize: setSp(14)),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Text(deliveryInfo["deliveryType"] as String,
                        style: TextStyle(
                            color: Color(0xff323232), fontSize: setSp(14))),
                  ),
                ),
              ],
            ),
          ),
          //快递单号
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: setWidth(64),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "快递单号:",
                    style: TextStyle(
                        color: Color(0xff323232), fontSize: setSp(14)),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Text(deliveryInfo["deliveryNumber"] as String,
                        style: TextStyle(
                            color: Color(0xff323232), fontSize: setSp(14))),
                  ),
                ),
                SizedBox(
                  width: setWidth(31),
                ),
                GestureDetector(
                  onTap: () {},
                  child: Container(
                    child: Text(
                      "修改",
                      style: TextStyle(
                          color: Color(0xff4D8ADD), fontSize: setSp(14)),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
